﻿@using Kendo.Mvc.UI
<div><img src="~/images/cdmhero.png" /></div>
<div style="width:1175px;margin-top:10px;">
    <div style="margin-bottom:10px;"><input style="width:300px;" class=k-textbox type=text id="txtSearchString" placeholder="Search for Files..." />&nbsp;
    <button style="border:1px solid grey;" id="btnSearch">Search</button>
</div>
    @(Html.Kendo().Grid<CdmDeviceHealthMvc.Models.StatusItem>()
       .Name("grid")
       .Columns(columns =>
       {           
           columns.Bound(c => c.ComputerName).Encoded(false).Title("Computer Name");
           columns.Bound(c => c.SoftwarePackageId).Title("Software Package Id");
           columns.Bound(c => c.SoftwarePackageName).Title("Software Package Name");
           /*columns.Bound(c => c.FileName).Title("File Name");*/
           columns.Command(command => { command.Custom("Revoke").Click("RevokePackage"); });
       })
           .Editable(editable => editable.Mode(GridEditMode.InLine))
           .Pageable()
           .Sortable()
           .Filterable()
           .Scrollable()
           .HtmlAttributes(new { style = "height:580px;" })
           .Events(events => events.Edit("onedit"))
           .DataSource(dataSource => dataSource
               .Ajax()
               .PageSize(30)
               .Events(events => events.Error("error_handler"))
               .Model(model => model.Id(c => c.SoftwarePackageId))
               .Update(update => update.Action("EditingInline_Update", "Status"))
               .Read(read => read.Action("EditingInline_Read", "Status"))
 ))
</div>
<script type="text/javascript">
        function error_handler(e) {
            if (e.errors) {
                var message = "Errors:\n";
                $.each(e.errors, function (key, value) {
                    if ('errors' in value) {
                        $.each(value.errors, function () {
                            message += this + "\n";
                        });
                    }
                });
                alert(message);
            }
        }
        function onedit(item) {
        }
        $(document).ready(function () {
            $("#btnSearch").kendoButton({
                click: onSearch
            })
        });
        function RevokePackage(e) {

            var row = $(e.target).closest("tr");
            var grid = $("#grid").data("kendoGrid");
            var dataItem = grid.dataItem(row);
      
            $.ajax({
                url: "Status/EditingInline_Update",
                data: {        
                    SoftwarePackageId: dataItem.SoftwarePackageId
                },
                dataType: "json",
                type: "POST",
                error: function () {
                    alert("Unexpected Error Occurred");
                },
                success: function (data) {
                    alert("Software Package \"" + dataItem.SoftwarePackageName + "\" was successfully revoked.");
                    $("#grid").data("kendoGrid").dataSource.read();
                }
            });

        }
        function onSearch() {
            var q = ";" + $("#txtSearchString").val() + ";";
            var grid = $("#grid").data("kendoGrid");
            grid.dataSource.query({
                page: 1,
                pageSize: 20,
                filter: {
                    logic: "or",
                    filters: [
                      { field: "FileName", operator: "contains", value: q }
                    ]
                }
            });
        }

</script>
